package com.example.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.*;
import java.time.LocalDateTime;
import java.util.List;

/**
 * Elasticsearch文档实体类
 * 对应ES中的一个文档
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@org.springframework.data.elasticsearch.annotations.Document(indexName = "documents")
@Setting(shards = 1, replicas = 0)
public class Document{

    @Id
    private String id;

    /**
     * 标题字段 - 支持IK分词器
     * analyzer: 索引时使用的分析器
     * searchAnalyzer: 搜索时使用的分析器
     */
    @Field(type = FieldType.Text, 
           analyzer = "ik_max_word", 
           searchAnalyzer = "ik_smart")
    private String title;

    /**
     * 内容字段 - 支持IK分词器
     */
    @Field(type = FieldType.Text, 
           analyzer = "ik_max_word", 
           searchAnalyzer = "ik_smart")
    private String content;

    /**
     * 作者字段 - 既支持精确查询又支持分词查询
     */
    @MultiField(
        mainField = @Field(type = FieldType.Text, analyzer = "ik_smart"),
        otherFields = {
            @InnerField(suffix = "keyword", type = FieldType.Keyword)
        }
    )
    private String author;

    /**
     * 标签列表
     */
    @Field(type = FieldType.Keyword)
    private List<String> tags;

    /**
     * 创建时间
     */
    @Field(type = FieldType.Date, format = DateFormat.date_hour_minute_second)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    @Field(type = FieldType.Date, format = DateFormat.date_hour_minute_second)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;

    /**
     * 阅读数
     */
    @Field(type = FieldType.Integer)
    private Integer readCount;

    /**
     * 状态：1-发布，0-草稿
     */
    @Field(type = FieldType.Integer)
    private Integer status;

    /**
     * 构造函数 - 方便创建测试数据
     */
    public Document(String title, String content, String author, List<String> tags) {
        this.title = title;
        this.content = content;
        this.author = author;
        this.tags = tags;
        this.createTime = LocalDateTime.now();
        this.updateTime = LocalDateTime.now();
        this.readCount = 0;
        this.status = 1;
    }
}